package fusion;

import java.io.*;
import java.util.HashMap;

class Foo implements Serializable {
    int a;
    int b;
    int c;
    public Foo(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public Foo() {
        a = 1;
        b = 2;
        c = 3;
    }
    public void printInfo() {
        System.out.println("a: " + a + ", b: " + b + ", c: " + c);
    }
}

class Bar implements Serializable {
    int a;
    int b;
    byte c;

    public Bar(int a, int b) {
        this.a = a;
        this.b = b;
        c = 100;
    }
    public Bar() {
        a = 3;
        b = 2;
        c = 1;
    }
    public void printInfo() {
        System.out.println("a: " + a + ", b: " + b + ", c: " + c);
    }
}



public class ObjFusion {
    public static void main(String[] args) {
        Foo foo = new Foo();
        Bar bar = new Bar();
        Bar bar1 = new Bar(10, 11);
        Bar bar2 = new Bar(100, 110);
        Foo foo2 = new Foo(10, 11, 12);
        Foo foo3 = new Foo(100, 110, 120);


        byte[] baFoo = FusionUtil.Object2ByteArray(foo);
        byte[] baFoo2 = FusionUtil.Object2ByteArray(foo2);
        byte[] baBar = FusionUtil.Object2ByteArray(bar);

        int[] iaFoo = FusionUtil.ByteArray2IntArray(baFoo);
        int[] iaFoo2 = FusionUtil.ByteArray2IntArray(baFoo2);
        int[] iaBar = FusionUtil.ByteArray2IntArray(baBar);


        // fusion

        //code = Fusion(code, iaFoo, iaBar, 1);

        byte[] baFooRet = FusionUtil.IntArray2ByteArray(iaFoo, baFoo.length);
        byte[] baFooRet2 = FusionUtil.IntArray2ByteArray(iaFoo2, baFoo2.length);
        byte[] baBarRet = FusionUtil.IntArray2ByteArray(iaBar, baBar.length);


        foo = null;
        foo = (Foo)FusionUtil.ByteArray2Object(baFooRet);
        foo2 = null;
        foo2 = (Foo)FusionUtil.ByteArray2Object(baFooRet2);
        bar = null;
        bar = (Bar)FusionUtil.ByteArray2Object(baBarRet);

        if(foo != null)
            foo.printInfo();
        if(foo2 != null)
            foo2.printInfo();
        if(bar != null) 
            bar.printInfo();
        

        FusionEntry f1 = new FusionEntry(3, 1, 0);
        //FusionEntry f2 = new FusionEntry(3, 2, 1);
        
        f1.updateCodes(null, foo, 0);
        f1.updateCodes(null, foo2, 1);
        f1.updateCodes(null, bar, 2);
        f1.updateCodes(bar, foo3, 2);
        f1.updateCodes(foo3, bar, 2);
 //       f1.updateCodes(foo3, null, 2);


        //f2.updateCodes(null, foo, 0);
        //f2.updateCodes(null, foo2, 1);
        //f2.updateCodes(null, bar, 2);
        //f2.updateCodes(bar, foo3, 2);
        //f2.updateCodes(foo3, null, 2);
        
        foo.printInfo();
        foo2.printInfo();
        bar.printInfo();


        int[] idx = new int[3];
        idx[0] = 0; 
        idx[1] = 1; 
        idx[2] = 2; 

        HashMap<Integer, Serializable> mapPrimariesObj = new HashMap<Integer, Serializable>();
        mapPrimariesObj.put(1, foo2);
        mapPrimariesObj.put(2, bar);
       
        
        int[][] code = new int[1][];
        code[0] = null;
        //code[1] = f2.getCode();



        Object[] objs = f1.getRecoveredData(idx, code, mapPrimariesObj);
        foo = (Foo) objs[0];
        foo2 = (Foo) objs[1];
        bar = (Bar) objs[2];
//        foo3 = (Foo) objs[2];
        foo.printInfo();
        foo2.printInfo();
        bar.printInfo();
 //       foo3.printInfo();
    }
}
